home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / whohas.arc / WHOHAS2.C < prev    next >
C/C++ Source or Header  |  1988-10-19  |  3KB  |  98 lines

  1. /* *********************************************************************
  2.  * WHOHAS is a utility to determine whether or not someone else on the
  3.  * network is using a file, and if they are to alert them to the fact
  4.  * that you would like to use the file.
  5.  ***********************************************************************/
  6. #include <stdio.h>
  7. #include <nit.h>
  8.  
  9. char    path[255], server[48];        /* for parsing path */
  10. char    volume[16], directories[255];
  11.  
  12. /* for conns. using file call */
  13. WORD    connectionID;                /* server ID number */
  14. BYTE    directoryHandle;            /* handle number at server */
  15. char    filePath[255];                
  16. int        lastRecord, taskID;            /* end of reply/buffers indicators */
  17. CONN_USING_FILE    fileUse;            /* the data structure */
  18.  
  19. /* for connection info. call */
  20. char    objectName[48];                /* bindery name of user */
  21. int        objectType;
  22. long    objectID;
  23. BYTE    loginTime[7];
  24.  
  25. /* for send message call */
  26. char    message[56];                /* place for message text */
  27. WORD    connectionList[100];        /* list of workstation connection IDs */
  28. BYTE    resultList[100];            /* completion codes for broadcast */
  29. WORD    connectionCount;
  30.  
  31. ErrorOut(char *errorString, int errorLevel)
  32. {
  33.     printf("%s\n", errorString);
  34.     exit(errorLevel);
  35. }
  36.  
  37. main(int argc, char *argv[])
  38. {
  39.     unsigned    ccode;
  40.     char        ch;
  41.     int         i;
  42.  
  43.     if (argc!=2)
  44.     {
  45.         printf("Usage: whohas filename.\n");
  46.         exit(0);
  47.     }
  48.     strcpy(filePath, argv[1]);
  49.     if (ConvertNameToFullPath(filePath, path))
  50.         ErrorOut("Invalid filename/path.", 1);
  51.  
  52.     ParsePath(filePath, server, volume, directories);
  53.     if (strlen(server)==0)
  54.         ErrorOut("Please indicate a file on a network drive.", 1);
  55.  
  56.     if (GetConnectionID(server, &connectionID))
  57.         ErrorOut("Invalid server.", 1);
  58.  
  59.     directoryHandle = 0;            /* use null handle since path is full */
  60.     taskID = lastRecord = 0;        /* get first buffer in first reply */
  61.     connectionCount =  0;            /* start counting connections from 0 */
  62.     do
  63.     {                /* here's where all the real work gets done */
  64.         ccode = GetConnectionsUsingFile(connectionID, &lastRecord, &taskID,
  65.                      directoryHandle, path, sizeof(fileUse), &fileUse);
  66.         if (fileUse.logicalConnNumber)
  67.             connectionList[connectionCount++] = fileUse.logicalConnNumber;
  68.     } while (lastRecord);
  69.     
  70. /* if file is in use, print users and allow message send */
  71.     if (connectionCount)
  72.     {                /* direct these bindery requests to correct server */
  73.         SetPreferredConnectionID(connectionID);
  74.         for (i=0; i<connectionCount; i++)
  75.         {
  76.             ccode = GetConnectionInformation(connectionList[i], objectName,
  77.                          &objectType, &objectID, loginTime);
  78.             printf("User %s is using file %s.\n", objectName, filePath);
  79.         }
  80.         printf("Send a message to persons using the file? (y/n)");
  81.         while(!kbhit())
  82.             ;
  83.         ch = getche();        /* get & echo response */
  84.         printf("\n");
  85.         if (ch=='y' || ch=='Y')
  86.         {                 /* message can go to end of line */
  87.             printf("Enter a short message =>");
  88.             gets(message);        /* send the message to the whole list */
  89.             ccode = SendBroadcastMessage(message, connectionList, resultList,
  90.                         connectionCount);
  91.             /* I ignore the resultList of completion codes */
  92.         }
  93.     }
  94.     else
  95.         printf("Nobody is currently using file %s.\n", filePath);
  96.     return(0);
  97. }
  98.